<?php
class giftControl extends skymvc{
	public function __construct(){
		parent::__construct();	
	}
	
	public function onInit(){
		
	}
	
	public function onDefault(){
		M("login")->checkLogin();
		$t_userid=get('t_userid','i');
		$f_userid=M("login")->userid;
		if(empty($t_userid) || $t_userid==$f_userid){
			$this->goAll("请选择送礼对象",1);
		}
		$data=M("gift")->select(array(
			"where"=>" status=1 "
		));
		$this->smarty->Assign(array(
			"t_userid"=>$t_userid,
			"data"=>$data,
			"catlist"=>M("gift")->catlist()
		));
		$content=$this->smarty->fetch("gift/index.html");
		$this->goAll("success",0,$content);
	}
	
	 
	
	
	public function onSend(){
		M("login")->checkLogin();
		$gift_id=get('id','i');
		$f_userid=M("login")->userid;
		$t_userid=get('t_userid','i');
		$num=max(1,get('num','i'));
		if(empty($t_userid) || $t_userid==$f_userid){
			$this->goAll("请选择送礼对象",1);
		}
		$user=M("user")->selectRow("userid=".$f_userid);
		$gift=M("gift")->selectRow("id=".$gift_id." AND status=1");
		if(empty($gift)){
			$this->goAll("礼物已下架",1);
		}
		$money=$gift['price']*$num;
		if($user['money']<$money){
			$this->goAll("余额不足，请先充值",1111);
		}
		M("user")->addMoney(array(
			"userid"=>$f_userid, 
			"money"=>-$money,
			"grade"=>$num*$gift['grade'],
			"content"=>"你购买“".$gift['title']."”,花了".$money.","
		));
		M("gift_send")->insert(array(
			"f_userid"=>$f_userid,
			"t_userid"=>$t_userid,
			"gift_id"=>$gift_id,
			"dateline"=>time(),
			"num"=>$num
		));
		/****经验值****/
	 
		M("user")->addMoney(array(
			"userid"=>$t_userid, 
			"grade"=>$num*$gift['grade'],
			"content"=>"你收到“".$gift['title']."”,获得了".($num*$gift['grade'])."积分,"
		));
		$row=M("gift_accept")->selectRow("userid=".$t_userid." AND gift_id=".$gift_id." ");
		if(empty($row)){
			M("gift_accept")->insert(array(
				"userid"=>$t_userid,
				"gift_id"=>$gift_id,
				"num"=>$num
			));
		}else{
			M("gift_accept")->update(array(
				"num"=>$row['num']+$num
			),"id=".$row['id']);
		}
		if($zbliveid=get_post('zbliveid')){
			$zblive=M("mod_zblive")->selectRow("id=".$zbliveid);
			M("mod_zblive")->update(array("gift_num"=>$zblive['gift_num']+$num,"total_money"=>$zblive['total_money']+$money),"id=".$zbliveid);
		}
		$this->goAll("发送成功");	
	}
	
	public function onMy(){
		$rscount=true;
		$userid=M("login")->userid;
		switch(get('type')){
			case "1":
				$where=" f_userid=".$userid;
				break;
			default:
					$where=" t_userid=".$userid;
				break;
		}
		
		$url="/index.php?m=gift&a=my";
		
		$start=get_post('per_page','i');
		$limit=20;
		$option=array(
			"where"=>$where,
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC"
		);

		$data=M("gift_send")->select($option,$rscount);
		if($data){
			foreach($data as $v){
				$ids[]=$v['gift_id'];
				if(get('type') == 1){
					$uids[]=$v['t_userid'];
				}else{
					$uids[]=$v['f_userid'];
				}
				
			}
			$us=M("user")->getUserByIds($uids);
			$gifts=M("gift")->getListByIds($ids);
			foreach($data as $k=>&$v){
				$v['title']=$gifts[$v['gift_id']]['title'];
				if(get('type') == 1){
					$v['nickname']=$us[$v['t_userid']]['nickname'];
				}else{
					$v['nickname']=$us[$v['f_userid']]['nickname'];
				}
				
			}
		}
		$this->smarty->goAssign(array(
			"data"=>$data
		));
		$tpl=M("pagetpl")->get("gift","my");
		$this->smarty->display($tpl);
		$this->smarty->display("gift/my.html");
	}
	
}
